import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.Data;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.Map;

/**
 * @author LHM
 * @Tips 热爱每一天，奋斗每一天
 * @date 2020/5/30 - 23:08
 */
@SpringBootTest
public class Excel {

    public static void main(String[] args) {
        String path = "F:\\write.xlsx";
        ArrayList<DemoData> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            DemoData data = new DemoData();
            data.setName("lhm");
            data.setNum(123);
            list.add(data);
        }

        //调用方法实现写操作
        //第一个参数为文件的路径，第二个参数为实体类的类型
        EasyExcel.write(path,DemoData.class).sheet("学生列表").doWrite(list);
    }

    @Test
    public void test(){
        EasyExcel.read("F:\\write.xlsx", DemoData.class ,new EventListener()).sheet().doRead();
    }
}

@Data
class DemoData{

    @ExcelProperty(value = "学生名", index = 0)
    private String name;

    //第二列
    @ExcelProperty(value = "学生编号", index = 1)
    private Integer num;
}

class EventListener extends AnalysisEventListener<DemoData>{

    @Override
    public void invoke(DemoData demoData, AnalysisContext analysisContext) {
        System.out.println("数据是:****" + demoData);
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        System.out.println("表头为：" + headMap);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {}
}
